package com.atguigu.day10;

import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.table.api.bridge.java.StreamTableEnvironment;

public class Flink01_SQL_GroupWindow {
    public static void main(String[] args) {
        //1.获取流的执行环境
        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();
        env.setParallelism(1);

        //2.获取表的执行环境
        StreamTableEnvironment tableEnv = StreamTableEnvironment.create(env);

        tableEnv.executeSql("create table sensor(" +
                "id string," +
                "ts bigint," +
                "vc int, " +
                "t as to_timestamp_ltz(ts,3)," +
                "watermark for t as t - interval '5' second)" +
                "with("
                + "'connector' = 'filesystem',"
                + "'path' = 'input/sensor-sql.txt',"
                + "'format' = 'csv'"
                + ")");


        //TODO 3.在sql中使用GroupWindow
        tableEnv.executeSql("select " +
                " id," +
                " sum(vc) as vcSum," +
                " tumble_start(t,interval '3' second) as wStart," +
                " tumble_end(t,interval '3' second) as wEnd" +
                " from sensor" +
                " group by " +
                " id," +
                " Tumble(t,interval '3' second)"
        ).print();

    }
}
